package com.server.server_a10.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.server.server_a10.entity.Room;
import com.server.server_a10.entity.User;
import com.server.server_a10.management.entity.RoomInf;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 这个接口用来对接房间数据库操作
 */
@Mapper
public interface RoomMapper extends BaseMapper<User> {
    @SelectProvider(type = ProviderRoom.class, method = "getRoomByHomeId")
    List<Room> getRoomByHomeId(long home_id);

    @SelectProvider(type = ProviderRoom.class, method = "getRoomCount")
    int getRoomCount();

    @SelectProvider(type = ProviderRoom.class, method = "getRoomLimit")
    List<RoomInf> getRoomLimit(String name, int index, int limit,String home_id);
    @SelectProvider(type = ProviderRoom.class, method = "getRoomLimitCount")
    int getRoomLimitCount(String name);

    @DeleteProvider(type = ProviderRoom.class,method = "delRoomById")
    int delRoomById(int id);

    @InsertProvider(type = ProviderRoom.class, method = "insRoom")
    int insRoom(String name,int home_id);

    class ProviderRoom {
        public static String getRoomByHomeId() {
            return "select * from room where home_id=#{home_id}";
        }

        public static String getRoomCount(){
            return "select count(id) from room";
        }

        //根据id和limit，index来获取信息（需要的name参数实际上是id）
        public static String getRoomLimit(){
            return "select r.id as room_id,r.home_id as home_id,u.id as user_id,u.name as user_name,\n" +
                    "       (SELECT COUNT(*)\n" +
                    "        FROM garden g\n" +
                    "                 JOIN light l ON g.room_id = l.room_id\n" +
                    "                 JOIN smog s ON g.room_id = s.room_id\n" +
                    "                 JOIN heating_equipment h ON h.room_id = g.id\n" +
                    "        WHERE g.room_id = r.id) as equipment_count\n" +
                    "from room r\n" +
                    "    left join home h on r.home_id = h.id\n" +
                    "    left join user u on h.master_id = u.id\n" +
                    "where r.id like CONCAT('%',#{name},'%') and r.home_id like concat('%', #{home_id} ,'%') limit #{index},#{limit}";
        }

        public static String getRoomLimitCount(){
            return "select count(*) from room r\n" +
                    "         left join home h on r.home_id = h.id\n" +
                    "         left join user u on h.master_id = u.id\n" +
                    "where r.id like CONCAT('%',#{name},'%')";
        }

        public static String delRoomById(){
            return "delete from room where id=#{id}";
        }

        public static String insRoom(){
            return "insert into room (name, home_id, create_time, update_time)\n" +
                    "values (#{name},#{home_id},now(),now());";
        }
    }
}
